#!/bin/bash

# Ruoyi-ai Application Restart Script

APP_HOME=$(cd "$(dirname "$0")" && pwd)

echo "Restarting Ruoyi-ai Application..."

# 停止应用
if [ -f "$APP_HOME/stop.sh" ]; then
  "$APP_HOME/stop.sh"
else
  echo "Stop script not found. Trying to stop via PID file..."
  PID_FILE="$APP_HOME/app.pid"
  if [ -f "$PID_FILE" ]; then
    PID=$(cat "$PID_FILE")
    if ps -p $PID > /dev/null; then
      kill $PID
      while ps -p $PID > /dev/null; do
        sleep 1
      done
      rm -f "$PID_FILE"
      echo "Application stopped."
    fi
  fi
fi

# 等待几秒确保应用完全停止
sleep 3

# 启动应用
if [ -f "$APP_HOME/start.sh" ]; then
  "$APP_HOME/start.sh"
else
  echo "Start script not found."
  exit 1
fi

echo "Ruoyi-ai Application restarted successfully."